/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package gui.views;

import gui.Model;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class StatisticsFrame extends JFrame{

	private int width = 700;
	private int height = 600;
	private Model model;
	private DefaultTableModel dtmStatistics;
	
	
	public StatisticsFrame(Model model){
		this.model = model;
		
		// Get screen size
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

		// Set window title
	    this.setTitle("HRL-Simulation Statistics");
		
		// Set window size
		this.setSize(this.width, this.height);
		    
		// Get the center of you screen
		int w = this.getSize().width;
		int h = this.getSize().height;
		int x = (dimension.width-w)/2;
		int y = (dimension.height-h)/2-20;
		    
		// Set the window location
		this.setLocation(x, y);
		
		this.setLayout(new BorderLayout());
		this.setResizable(false);
		
		this.setVisible(false);
		
		this.createStatisticsScrollPane();
	}
	
	
	/**
	 * 
	 */
	private void createStatisticsScrollPane() {
		// Create default table model
		this.dtmStatistics = new DefaultTableModel(){
		    public boolean isCellEditable(int rowIndex, int mColIndex) {
		        return false;
		    }
		};
		
		// Add table header
		for(int i=0; i<this.model.getTableHeaderStatistics().length; i++){
			this.dtmStatistics.addColumn(this.model.getTableHeaderStatistics()[i]);
		}
		
		// Create table
		JTable jtStatisticsTable = new JTable(this.dtmStatistics);
		jtStatisticsTable.getTableHeader().setReorderingAllowed(false);
		jtStatisticsTable.getColumnModel().getColumn(0).setPreferredWidth(160);
		
		// Create scroll pane for loads table
		JScrollPane jspStatistics = new JScrollPane(jtStatisticsTable);
		this.add(jspStatistics);
	}
	
	
	/**
	 * 
	 * @param tableData
	 */
	public void addStatisticsData(String[][] tableData){
		String[] row = new String[tableData[0].length];
		
		// Remove old entries
		while (this.dtmStatistics.getRowCount() > 0) {
			this.dtmStatistics.removeRow(0);
	    }
		
		// Add new entries
		for(int i=0; i<tableData.length; i++){
			for(int j=0; j<tableData[i].length; j++){
				row[j] = tableData[i][j];
			}
			this.dtmStatistics.addRow(row);
		}
	}
}
